package com.itheima.sh.consumer.controller;

import com.itheima.sh.consumer.client.UserFeignApi;
import com.itheima.sh.consumer.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2021/5/15 11:09
 **/
@RestController
@RequestMapping("consumer")
@Slf4j
public class ConsumerController {
    @Autowired
    UserFeignApi userFeignApi;
    @GetMapping("{id}")
    public Map queryById(@PathVariable String id){
        User user = userFeignApi.queryById(id);
        user.setName("小明");
        String save = userFeignApi.save(user);
        Map map = new HashMap();
        map.put("result",save);
        return map;
    }

//    @Autowired
//    RestTemplate restTemplate;
//    @GetMapping("{id}")
//    public Map queryById(@PathVariable String id){
//        // 从user-service服务获取 指定id的数据
//        //  GET http://localhost:8081/user/id
//        Map map = restTemplate.getForObject("http://localhost:8081/user/" + id, Map.class);
//        return map;
//    }

//    @Autowired
//    DiscoveryClient discoveryClient; // 注册中心客户端对象

//    @GetMapping("{id}")
//    public Map queryById(@PathVariable String id){
//        // 从user-service服务获取 指定id的数据
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        for (ServiceInstance instance : instances) {
//            System.out.println("host: " + instance.getHost());
//            System.out.println("port: " + instance.getPort());
//        }
//        ServiceInstance serviceInstance = instances.get(0);
//        String url = "http://"+serviceInstance.getHost() + ":"+serviceInstance.getPort() +"/user/";
//        //  GET http://localhost:8081/user/id
//        Map map = restTemplate.getForObject(url + id, Map.class);
//        return map;
//    }
//    @GetMapping("{id}")
//    @HystrixCommand(fallbackMethod = "queryByIdFallback",threadPoolProperties = {@HystrixProperty(name = "coreSize",value = "2")})
//    public Map queryById(@PathVariable String id){
//        String url = "http://user-service/user/";
//
//        if ("itheima".equals(id)) {
//            throw new RuntimeException("itheima");
//        }
//
//        Map map = restTemplate.getForObject(url + id, Map.class);
//        return map;
//    }
//    public Map queryByIdFallback(String id){
//        log.error("服务降级方法触发  ==> {}",id);
//        Map map = new HashMap();
//        map.put("msg","当前服务请求繁忙,请稍后重试");
//        return map;
//    }
}
